import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GetPanel extends JPanel implements ActionListener {
    private ATM atm;
    private JButton sureBtn;
    private JButton clearBtn;
    private Icon sureImg = Util.getImageIcon("/images/buttonSure.jpg");
    private Icon clearImg = Util.getImageIcon("/images/buttonOff.jpg");
    private JLabel get = new JLabel("请输入取款金额：");
    private JTextField getmoney = new JTextField();

    public GetPanel(ATM atm) {
        this.atm = atm;
        this.setLayout(null);
        this.setOpaque(false);

        get.setBounds(200, 200, 200, 30);
        get.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(get);
        getmoney.setBounds(420, 200, 200, 30);
        this.add(getmoney);

        sureBtn = new JButton(sureImg);
        sureBtn.setBounds(200, 240, 140, 50);
        this.add(sureBtn);
        sureBtn.addActionListener(this);

        clearBtn = new JButton(clearImg);
        clearBtn.setBounds(420, 240, 140, 50);
        this.add(clearBtn);
        clearBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn) {
            if (Double.valueOf(getmoney.getText()) > atm.getUsers().get(atm.getNum()).getMoney()) {
                JOptionPane.showMessageDialog(null, "你没有那么多钱，请重新输入");
            } else {
                JOptionPane.showMessageDialog(null, "本次取款金额为：" + getmoney.getText() + "元，余额为：" + (atm.getUsers().get(atm.getNum()).getMoney() - Double.valueOf(getmoney.getText())));
                atm.getUsers().get(atm.getNum()).setMoney(atm.getUsers().get(atm.getNum()).getMoney() - Double.valueOf(getmoney.getText()));
                String sql="update t_user set u_money="+atm.getUsers().get(atm.getNum()).getMoney()+"where u_name='"+atm.getUsers().get(atm.getNum()).getUsername()+"'";
                Util.insert(sql);
                atm.switchPanel("menu");
            }

        } else if (e.getSource() == clearBtn) {
            atm.switchPanel("menu");
        }
    }
}
